#include<stdio.h>

#include<Mesh_IO/Mesh_IO.h>



#define _ReadPcd_ LibCell_ReadPcd_
#define quote lib_cell_quote


void static inline binary_read_one_line(FILE* infile,char* str)
{
    memset(str,0,sizeof(char)*300);
    char temp='\0';

    fread(&temp,sizeof(char),1,infile);
    int i=0;
    while( temp!='\n' )
    {
        //printf("%d %c\n",i,temp);
        str[i]=temp;
        i++;
        if(feof(infile))
        {
            return ;
        }
        fread(&temp,sizeof(char),1,infile);
    } 
    if(str[i-1]=='\r')
    {
        str[i-1]='\0';
    }
}

float*  my_read_pcd (const char  *filename,int *num)
{
    printf("begin read pcd\n");
    int result;
#ifdef _WIN32
    FILE *infile;
    fopen_s(&infile,filename,"rb");
#else
    FILE *infile=fopen(filename,"rb");
#endif
    if(!infile)
    {
        printf("can't open this file %s\n",filename);
    }
    fseek(infile,0,SEEK_SET); 

    char str[300]={0},str1[300]={0},temp_str[300]={0};
    char str2[300]={0},str3[300]={0};

    // binary_read_one_line(infile,str);
    // printf("%s\n",str);

    *num=0;
    while(!feof(infile)&&!(strcmp(str,"DATA ascii")==0|| strcmp(str,"DATA binary")==0  ))
    {
        binary_read_one_line(infile,str);
        printf("%s\n",str);

        if(str[0]=='#')
        {
            continue;
        }

        if(str[0]=='P'&&str[1]=='O'&&str[2]=='I'&&str[3]=='N'&&str[4]=='T'&&str[5]=='S')
        {
            sscanf(&(str[7]), "%d", num );
        }
        else if(str[0]=='F'&&str[1]=='I'&&str[2]=='E'&&str[3]=='L'&&str[4]=='D'&&str[5]=='S')
        {
            strcpy(str1,&(str[7])); 

        }
        else if( str[0]=='S'&&str[1]=='I'&&str[2]=='Z'&&str[3]=='E' )
        {
            strcpy(str2,&(str[5])); 
        }
        else if(str[0]=='C'&&str[1]=='O'&&str[2]=='U'&&str[3]=='N'&&str[4]=='T'  )
        {
            strcpy(str3,&(str[6]) );

        }
        // else if ()
        // {

        // }
    }

    printf("%s %s\n",str1,str2);
    int n_str1,n_str2,n_str3,ns[10]={0},ns1[10]={0};
    char** str11= split(str1,' ',&n_str1);
    char** str22= split(str2,' ',&n_str2);
    char** str33=split(str3,' ',&n_str3); 
    for(int i=0;i<n_str2;i++)
    {
        sscanf(str22[i],"%d",&(ns[i]) );
        sscanf(str33[i],"%d",&(ns1[i]) );
        //printf("%d \n",ns[i]);
    }

    if(feof(infile))
    {
        return NULL;
    }
    float * re=(float*)malloc(sizeof(float)*3*(*num));
    printf("num: %d\n",*num);
    float p[3]={0};double dp[3]={0};
    if(strcmp(str,"DATA ascii")==0 )
    {

        for(int i=0;i<*num;i++)
        {
            for(int j=0;j<n_str1;j++)
            {
                if( strcmp(str11[j],"x" )==0 )
                {
                    fscanf(infile,"%f",&(p[0])); 
                }  
                else if(strcmp(str11[j],"y")==0)
                {
                    fscanf(infile,"%f",&(p[1]));
                }
                else if(strcmp(str11[j],"z")==0)
                {
                    fscanf(infile,"%f",&(p[2]));
                }
                else 
                {
                    fscanf(infile,"%s", temp_str);
                    memset(temp_str,0,sizeof(char)*300);

                }
               

            } 
            //dp[0]=p[0];dp[1]=p[1];dp[2]=p[2];
            memmove(&(re[i*3]),p,sizeof(float)*3 );
            //printf("%f %f %f\n",p[0],p[1],p[2]);
        }
    }
    else if(strcmp(str,"DATA binary")==0)
    {

        for(int i=0;i<*num;i++)
        {

            for(int j=0;j<n_str1;j++)
            {
                if( strcmp(str11[j],"x" )==0 )
                {
                    fread(&(p[0]),sizeof(float),1,infile);
                }  
                else if(strcmp(str11[j],"y")==0)
                {
                    fread(&(p[1]),sizeof(float),1,infile);
                }
                else if(strcmp(str11[j],"z")==0)
                {
                    fread(&(p[2]),sizeof(float),1,infile);
                }
                else 
                {
                    fread(temp_str,ns[j] ,ns1[j],infile); 
                    memset(temp_str,0,sizeof(char)*300);

                }
            } 
            memmove(&(re[i*3]),p,sizeof(float)*3 );
            //printf("%f %f %f\n",p[0],p[1],p[2]);
            
            //dp[0]=p[0];dp[1]=p[1];dp[2]=p[2];
            
        }
    }

    for(int i=0;i<n_str1;i++)
    {
        free(str11[i]);
    }   
    for(int i=0;i<n_str2;i++)
    {
        free(str22[i]);
    }
    for(int i=0;i<n_str3;i++)
    {
        free(str33[i]);
    }
    free(str11);free(str22);free(str33);


    fclose(infile);

    return re;
}



int main()
{
	Mesh mesh;
	Mesh_init(&mesh);

	//_ReadPcd_(&mesh,"/home/libo/Documents/data/1658566020927745.pcd");
	//_ReadPcd_(&mesh,"/home/libo/Documents/data/zzzl.pcd");
	//_ReadPcd_(&mesh,"./92.pcd");
	printf("num_v: %d\n",mesh.num_v(&mesh));

    int n=0;

	//float* points= my_read_pcd("/home/libo/Documents/data/zzzl.pcd");
	//float* points1= my_read_pcd("/home/libo/Documents/data/1658566020927745.pcd");
    float* points1= my_read_pcd("./92.pcd",&n);
	printf("here end\n");

	

	Mesh_free(&mesh);	


	return 0;

}
